unit oSaveManager;

interface
uses
  Classes, SysUtils, oManager, oProcessItem, oProcessObjectConstants,
  oDataTypeConstants, oProcessObjects, XMLIntf, XMLDoc, oSerializer;

  type
    ESaveManager = class(Exception)

    end;

    TSaveManager = class (TManager)
    private
      xmlDocument : TXMLDocument;
      procedure buildPath(paPath : string);
    public
      function ProcessObject(paItem: TProcessItem): TProcessItem; override;
      constructor Create; overload;
      constructor Create(paMaster : TManager); overload;
      destructor Destroy; override;
      procedure test;
    end;

implementation

uses
  uOSPRand, oGeneratorData, uOSPDyn;

{ TSaveManager }

constructor TSaveManager.Create;
begin

end;

procedure TSaveManager.buildPath(paPath: string);
var
  list : TStringList;
  s1, s2 : string;
  i : integer;
begin

  list := TStringList.Create;
  list.Add(paPath);
  if (not DirectoryExists(ExtractFileDir(paPath))) then
    begin
      s1 := paPath;
      s2 := ExtractFileDir(s1);
      while ( s2 <> ExtractFileDrive(paPath) + '\') do
        begin
          list.Insert(0, s2);
          s2 := ExtractFileDir(s2);
          if DirectoryExists(s2) then
            break;
        end;
      for I := 0 to List.Count - 1 do
        begin
          s2 := list[i];
          if not DirectoryExists(s2) then
            MkDir(s2);
        end;
    end;
end;

constructor TSaveManager.Create(paMaster: TManager);
begin
  Create;
  aMaster := paMaster;
end;

destructor TSaveManager.Destroy;
begin

  inherited;
end;

function TSaveManager.ProcessObject(paItem: TProcessItem): TProcessItem;
var
  path : Tstring;
  xml : TProcessingXML;
  list : TProcessingList;
  node,hlpnode: IXMLNode;
  generatorData: TGeneratorData;
  genType: String;
  a,b,c,d: Double;
  probs, vals: pDoubleArr;
  num: integer;
begin
  if paItem.Data <> nil then
    if paItem.Data is TProcessingList then
      list := TProcessingList(paItem.Data);

  case paItem.Code of
    PO_SAVEMAN_SAVE_DATA : begin
      try
        path := TString(list.Parameters[0]);
        xml := TProcessingXML(list.Parameters[1]);
        buildPath(ExtractFilePath(path.String_));
        if xml <> nil then
          begin
            xmlDocument.SaveToFile(path.String_);
            Result := TProcessItem.create(PO_SUCCESS, nil);
          end
        else
        raise ESaveManager.Create('XML document not ready!');
      except
        on e : ESaveManager do Result := TProcessItem.create(PO_FAILURE, nil);
      end;
    end;
    PO_SAVEMAN_PREPARE_SAVE : begin
        xmlDocument := TSerializer.prepareXMLDoc;
        xmlDocument.Active := true;
        xmlDocument.ChildNodes.Clear;
        node := xmlDocument.AddChild('data');
        Result := TProcessItem.create(PO_SUCCESS, TProcessingXML.Create(node));
    end;

    SAVE_GENERATOR:
    begin
      if not(paItem.Data is TXMLGenerator) then raise ESaveManager.Create('');
      node := (paItem.Data as TXMLGenerator).Node.AddChild('generator');
      generatorData := (paItem.Data as TXMLGenerator).GeneratorData;

      //vsetky generatory maju toto spolocne, takze to vytiahnem von
      node.Attributes['type'] := (generatorData).Code;
      node.Attributes['Multiplier'] := (generatorData).Multiplier;
      node.Attributes['Constant'] := (generatorData).Constant;
      node.Attributes['Randomized'] := (generatorData).Randomized;

      case generatorData.Code of
      //nezabudni si spravne pretypovat
      //hore si musis usenut unitu s tym generatorovym typom
        genGamma:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TGammaGeneratorData).Mean;
          hlpnode := node.AddChild('Dispersion');
          hlpnode.Attributes['value'] := (generatorData as TGammaGeneratorData).Dispersion;
          hlpnode := node.AddChild('MinX');
          hlpnode.Attributes['value'] := (generatorData as TGammaGeneratorData).MinX;
          hlpnode := node.AddChild('MaxX');
          hlpnode.Attributes['value'] := (generatorData as TGammaGeneratorData).MaxX;
        end;

        genBeta:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TBetaGeneratorData).Mean;
          hlpnode := node.AddChild('Dispersion');
          hlpnode.Attributes['value'] := (generatorData as TBetaGeneratorData).Dispersion;
          hlpnode := node.AddChild('MinX');
          hlpnode.Attributes['value'] := (generatorData as TBetaGeneratorData).MinX;
          hlpnode := node.AddChild('MaxX');
          hlpnode.Attributes['value'] := (generatorData as TBetaGeneratorData).MaxX;
        end;

        genBinomial:
        begin
          hlpnode := node.AddChild('Number');
          hlpnode.Attributes['value'] := (generatorData as TBinomialGeneratorData).Number;
          hlpnode := node.AddChild('Probab');
          hlpnode.Attributes['value'] := (generatorData as TBinomialGeneratorData).Probab;
        end;

        genDiscrete:
        begin
          hlpnode := node.AddChild('Probs');
         // hlpnode.Attributes['value'] := (generatorData as TDiscreteGeneratorData).Probs;
          hlpnode := node.AddChild('Values');
         // hlpnode.Attributes['value'] := (generatorData as TDiscreteGeneratorData).Values;
        end;

        genErlang:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TErlangGeneratorData).Mean;
          hlpnode := node.AddChild('Dispersion');
          hlpnode.Attributes['value'] := (generatorData as TErlangGeneratorData).Dispersion;
        end;

        genExponential:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TExponentialGeneratorData).Mean;
        end;

        genLinear:
        begin
          hlpnode := node.AddChild('Probs');
        //  hlpnode.Attributes['value'] := (generatorData as TLinearGeneratorData).Probs;
          hlpnode := node.AddChild('Values');
        //  hlpnode.Attributes['value'] := (generatorData as TLinearGeneratorData).Values;
        end;

        genNormal:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TNormalGeneratorData).Mean;
          hlpnode := node.AddChild('Dispersion');
          hlpnode.Attributes['value'] := (generatorData as TNormalGeneratorData).Dispersion;
        end;

        genPoisson:
        begin
          hlpnode := node.AddChild('Mean');
          hlpnode.Attributes['value'] := (generatorData as TPoissonGeneratorData).Mean;
        end;

        genUniform:
        begin
          hlpnode := node.AddChild('From');
          hlpnode.Attributes['value'] := (generatorData as TUniformGeneratorData).From;
          hlpnode := node.AddChild('To');
          hlpnode.Attributes['value'] := (generatorData as TUniformGeneratorData).UniTo;
        end;
        else raise Exception.Create('Bad generator code.');
      end;
      {if generator is tUniform then
      begin
        node.Attributes['type'] := 'uniform';
        hlpnode := node.AddChild('From');
        //hlpnode.Attributes['value'] := (generator as tUniform).From;
        hlpnode := node.AddChild('To');
        //hlpnode.Attributes['value'] := (generator as tUniform).To;
      end;
      if generator is tPoisson then
      begin
        node.Attributes['type'] := 'poisson';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tPoisson).Mean;
      end;
      if generator is tNormal then
      begin
        node.Attributes['type'] := 'normal';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tNormal).Mean;
        hlpnode := node.AddChild('Dispersion');
        //hlpnode.Attributes['value'] := (generator as tNormal).Dispersion;
      end;
      if generator is tLinear then
      begin
        node.Attributes['type'] := 'linear';
        hlpnode := node.AddChild('Probs');
        //hlpnode.Attributes['value'] := (generator as tLinear).Probs;
        hlpnode := node.AddChild('Values');
        //hlpnode.Attributes['value'] := (generator as tLinear).Values;
      end;
      if generator is tGama then
      begin
        node.Attributes['type'] := 'gama';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tGama).Mean;
        hlpnode := node.AddChild('Dispersion');
        //hlpnode.Attributes['value'] := (generator as tGama).Dispersion;
        hlpnode := node.AddChild('MinX');
        //hlpnode.Attributes['value'] := (generator as tGama).MinX;
        hlpnode := node.AddChild('MaxX');
        //hlpnode.Attributes['value'] := (generator as tGama).MaxX;
      end;
      if generator is tExponential then
      begin
        node.Attributes['type'] := 'exponential';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tExponential).Mean;
      end;
      if generator is tErlang then
      begin
        node.Attributes['type'] := 'erlang';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tErlang).Mean;
        hlpnode := node.AddChild('Dispersion');
        //hlpnode.Attributes['value'] := (generator as tErlang).Dispersion;
      end;
      if generator is tDiscrete then
      begin
        node.Attributes['type'] := 'discrete';
        hlpnode := node.AddChild('Probs');
        //hlpnode.Attributes['value'] := (generator as tDiscrete).Probs;
        hlpnode := node.AddChild('Values');
        //hlpnode.Attributes['value'] := (generator as tDiscrete).Values;
      end;
      if generator is tBinomial then
      begin
        node.Attributes['type'] := 'binomal';
        hlpnode := node.AddChild('Number');
        //hlpnode.Attributes['value'] := (generator as tBinomal).Number;
        hlpnode := node.AddChild('Probab');
        //hlpnode.Attributes['value'] := (generator as tBinomal).Probab;
      end;
      if generator is tBeta then
      begin
        node.Attributes['type'] := 'beta';
        hlpnode := node.AddChild('Mean');
        //hlpnode.Attributes['value'] := (generator as tBeta).Mean;
        hlpnode := node.AddChild('Dispersion');
        //hlpnode.Attributes['value'] := (generator as tBeta).Dispersion;
        hlpnode := node.AddChild('MinX');
        //hlpnode.Attributes['value'] := (generator as tBeta).MinX;
        hlpnode := node.AddChild('MaxX');
        //hlpnode.Attributes['value'] := (generator as tBeta).MaxX;
      end; }
      result := nil;
    end;

  end;

end;

procedure TSaveManager.test;
begin

end;

end.

